$(document).ready(function() {
	

    /*** Confirm box ***/
	$('a.confirm-window').click(function() {
	
	    var confirmBox = $(this).attr('href');
	
	    document.getElementById("user_id_temp").value = document.getElementById("user_id").value;
	    document.getElementById("user_name_temp").value = document.getElementById("user_name").value;
	    document.getElementById("user_name_kata_temp").value = document.getElementById("user_name_kata").value;
	    document.getElementById("password_temp").value = document.getElementById("password").value;
	    document.getElementById("email_address_temp").value = document.getElementById("email_address").value;
	
	    //****** VALIDATION ******//
	    var flag = true;
	    //UserID
		if($("#user_id_temp").val() == '') 
		{
			//Blank or not
			$("#showMsgID").html("<i class='fa fa-times fa-3x' style='color:red;'></i>");
			flag = false;
		}
		else
		{	
			//Validate special characters
			var ID = $("#user_id_temp").val();
			if(!(/^[s]{1}[u]{1}[c]{1}[0-9]{3}$/.test(ID))) 
			{
				$("#showMsgID").html("<span id='error'>This user ID is not correct format</span>");
				flag = false;
			}
			else
			{
				var $id = $("#user_id").val();
				$.post("../ajax/m_users/check_user_id/", {data:{MUser:{user_id:$id}}}, function(data) {
					
					if (data == 1) {
						$("#showMsgID").html("<i class='fa fa-check fa-3x' style='color:#26A04C;'></i>");
					} else {
						flag = false;
						$("#showMsgID").html("<span id='error'>This user ID is already in use</span>");
					}
					if (flag){
				    	document.getElementById("submit").removeAttribute('disabled'); //Check True enable submit button
				    }
					else document.getElementById("submit").setAttribute('disabled', 'disabled');
					return;
		        });//end $.post
			}

		}
		
		//Username
		if($("#user_name_temp").val() == '') 
		{
			//Blank or not
			$("#showMsgUN").html("<i class='fa fa-times fa-3x' style='color:red;'></i>");
			flag = false;
		}
		else
		{	
			$("#showMsgUN").html("<i class='fa fa-check fa-3x' style='color:#26A04C;'></i>");			
		} 
	    
	    //Username Kata
		if($("#user_name_kata_temp").val() != '') 
		{
			var kana_user = $("#user_name_kata_temp").val();
			if (ZenKanaCheck(kana_user) == false) {
				$("#showMsgUNK").html("<span id='error'>Please enter full katakana characters</span>");
				flag = false;
			} 
			else 
			{
				$("#showMsgUNK").html("<i class='fa fa-check fa-3x' style='color:#26A04C;'></i>");
			}
		}
		else
			$("#showMsgUNK").html("<span id='error'></span>");
		
	    //Password
		if($("#password_temp").val() == '') 
		{
			//Blank or not
			$("#showMsgPass").html("<i class='fa fa-times fa-3x' style='color:red;'></i>");
			flag = false;
		}
		else
		{	
			$("#showMsgPass").html("<i class='fa fa-check fa-3x' style='color:#26A04C;'></i>");
		}
		
		//Email
		if($("#email_address_temp").val() == '') 
		{
			//Blank or not
			$("#showMsgEM").html("<i class='fa fa-times fa-3x' style='color:red;'></i>");
			flag = false;
		}
		else
		{	
			//Validate special characters
			var email = $("#email_address_temp").val();
			if(!(/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/.test(email))) 
			{
				$("#showMsgEM").html("<span id='error'>This email is not correct format</span>");
				flag = false;
			}
			else
			{
				var $email = $("#email_address").val();
				$.post("../ajax/m_users/check_email_address/", {data:{MUser:{email_address:$email}}}, function(data) {
					
					if (data == 1) {
						$("#showMsgEM").html("<i class='fa fa-check fa-3x' style='color:#26A04C;'></i>");
					} else {
						$("#showMsgEM").html("<span id='error'>This email is already in use</span>");
						flag = false;
					}
					if (flag){
				    	document.getElementById("submit").removeAttribute('disabled'); //Check True enable submit button
				    }
					else document.getElementById("submit").setAttribute('disabled', 'disabled');
					return;
		        });//end $.post
			}
		}
		
		//Control submit button base on valiadation
		if(flag)
	    {
	    	document.getElementById("submit").removeAttribute('disabled'); //Check True enable submit button
	    }
	    else
	    	document.getElementById("submit").setAttribute('disabled', 'disabled');
	    
		//****** END VALIDATION ******//
		
	    $(confirmBox).fadeIn("slow");
	    $('body').append('<div id="over"></div>');
	    $('#over').fadeIn(300);
	    
	});
	
	$(document).on('click', ".close-btn, #over", function() { 
	    $('#over, .confirm-form').fadeOut(300 , function() {
	        $('#over').remove();  
	    }); 

	});
	
	
	
	
});// End ready

